package com.lthlearn.controller;

import com.lthlearn.pojo.Result;
import com.lthlearn.service.CollectService;
import com.lthlearn.util.JWTUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 2942733239@qq.com
 * @date: 2024/11/7 19:58
 * @description: com.lthlearn.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 根据用户 id 和景点 id 查找当前用户的收藏的景点是否在收藏表
     *
     * @param token
     * @param placeId
     * @return
     */
    @GetMapping("check")
    public Result checkCollect(@RequestParam(value = "token") String token, @RequestParam(value = "placeId") Integer placeId) {
        Claims claims = JWTUtils.checkJWT(token);
        // 校验登录状态
        if (claims != null && claims.get("id") != null) {
            // 已登录
            return collectService.checkCollectByUserIdAndPlaceId((Integer) claims.get("id"), placeId);
        }
        // 未登录
        return new Result("401", "查询失败，未登录");
    }

    /**
     * 增加收藏
     *
     * @param token
     * @param placeId
     * @return
     */
    @PostMapping("add")
    public Result addCollect(@RequestParam(value = "token") String token, @RequestParam(value = "placeId") Integer placeId) {
        Claims claims = JWTUtils.checkJWT(token);
        // 校验登录状态
        if (claims != null && claims.get("id") != null) {
            // 已登录
            // System.out.println(claims.get("id"));
            return collectService.addCollect((Integer) claims.get("id"), placeId);
        }
        // 未登录
        return new Result("401", "查询失败，未登录");
    }

    /**
     * 删除收藏
     *
     * @param token
     * @param placeId
     * @return
     */
    @DeleteMapping("remove")
    public Result removeCollect(@RequestParam(value = "token") String token, @RequestParam(value = "placeId") Integer placeId) {
        Claims claims = JWTUtils.checkJWT(token);
        // 校验登录状态
        if (claims != null && claims.get("id") != null) {
            // 已登录
            // System.out.println(claims.get("id"));
            return collectService.removeCollect((Integer) claims.get("id"), placeId);
        }
        // 未登录
        return new Result("401", "查询失败，未登录");
    }

}
